//
//  CoreAnimationViewController.swift
//  UI
//
//  Created by admin on 14/9/18.
//  Copyright (c) 2014年 sinosun. All rights reserved.
//

import UIKit

class CoreAnimationViewController: UIViewController {
    var isLeft = true
    override func viewDidLoad() {
        super.viewDidLoad()

        
        // Do any additional setup after loading the view.
        var segment = UISegmentedControl(items: ["Fade","Over","Push","Reveal"])
        segment.selectedSegmentIndex = 0

        self.navigationItem.titleView = segment
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "go", style: UIBarButtonItemStyle.Bordered, target: self, action: "go")
        
        
    }
    
    
    func go(){

        println("---fffffff---")
        //创建对象
        var animation : CATransition = CATransition()
        //设置属性
        animation.delegate = self
        animation.duration = 1.0
       // animation.timingFunction = UIViewAnimationCurve.EaseInOut
        
        
        switch (self.navigationItem.titleView as UISegmentedControl).selectedSegmentIndex{
        case 0 :
            println(kCATransitionFade)
            animation.type = kCATransitionFade
        case 1:
            println(kCATransitionMoveIn)
            animation.type = kCATransitionMoveIn
        case 2:
            println(kCATransitionPush)
            animation.type = kCATransitionPush
        case 3:
            println(kCATransitionReveal)
            animation.type = kCATransitionReveal
        default:
            println()
            animation.type = kCATransitionReveal
            
        }
        
       
        if isLeft {
            animation.subtype = kCATransitionFromLeft
        }else {
            animation.subtype = kCATransitionFromRight
        }
        
       // println(self.view.subviews)

        //创建动画动作
        self.view.exchangeSubviewAtIndex(0, withSubviewAtIndex: 1)
        
//        
//        //向视图添加动画
        var view = self.view.viewWithTag(150)
        view?.exchangeSubviewAtIndex(0, withSubviewAtIndex: 1)
        
        view?.layer.addAnimation(animation, forKey: "nono")
//        
//        
         isLeft = !isLeft
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
